<?php

/** 
 * 后台:设备管理
 * @property Dev_mdl $dev
 * @property School_mdl $school
 * @property Dev_log_mdl $devLog
 */
class Dev extends Admin_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model('Dev_mdl','dev');
		$this->load->model('School_mdl','school');
		$this->load->model('Dev_log_mdl','devLog');
		$this->load->model('Admin_mdl','admin');
		
		
	}
	

	public function index(){
		$this->load->library('Search_tpl',NULL,'searchTpl');
		$schoolList = $this->school->getSchoolList();
		$where = array();
		$like  = array();
		$order = 'id';
		$sort  = 'DESC';
		$orders = array(
				'orderID'=>'id',
				'time'=>'createTime',
		);
		$input = $this->input->get(NULL,TRUE);
		foreach ($input as $k=>$v){
			$v = trim($v);
			if ($v===''){
				continue;
			}
			switch ($k) {
				case 'orderID':
					$where['id'] = (int)$v;
					break;
				case 'uid':
					$where['userID'] = (int)$v;
					break;
				case 'devName':
					$like['name'] = $v;
					break;
				case 'tradeState':
					if( $v!='ALL' ){
						$where['tradeState'] = $v;
					}
					break;
				case 'st':
					$where['createTime > '] = (float)strtotime($v);
					break;
				case 'et':
					$where['createTime < '] = (float)strtotime($v);
					break;
				case 'order' :
					$order = isset($orders[$v]) ? $orders[$v] : 'id';
					break;
				case 'sort':
					$sort = (int)$v===0 ? 'DESC' : 'ASC';
					break;
				case 'phone':
					$where['phone'] = (float)$v;
					break;
				case 'schoolID':
					$v = (int)$v;
					if( $v ){
						$where['schoolID'] = $v;
					}
			}
		}
		$query = array();
		$query['select']= '*';
		$query['from']  = MY_Model::T_DEV;
		$query['where'] = $where;
		$query['like']  = $like;
		$query['order'] = $order;
		$query['sort']  = $sort;
		$list = $this->searchTpl->query($query);
		
		$temp = array();
		$temp['formItems'] = array(
				'school',
				array('type'=>'dateRange','label'=>'添加时间','name'=>array('st','et')),
				array('type'=>'text','label'=>'设备名','name'=>'devName'),
		);
		$temp ['thList'] = array(
	            '设备ID',
	            '设备名',
	            '设备MAC',
	            '学校',
				'设备类型',
	            '添加时间',
		);
		$devTypes = array(1=>'饮水机',2=>'洗衣机');
		$callback = function($v)use($schoolList,$devTypes) {
			return array(
					'trData'=>array(
							'id'=>$v['id'],
							'type'=>$v['type'],
					),
					$v['id'],
					$v['name'],
					$v['mac'],
					isset($schoolList[$v['schoolID']]) ? $schoolList[$v['schoolID']]['name'] : '',
					$devTypes[$v['type']],
					date('Y-m-d H:i:s', $v['createTime']),
			);

		};
		$temp['tableTitle'] = '设备列表--共查到%s台设备--';
		$temp['header'] = $this->getViewHeaderData();
		$temp['schoolList'] = $schoolList;
		$temp['jsCtrl'] = 'devIndexCtrl';
		$this->searchTpl->show($temp,$callback);
	}
	
	public function detail(){
		if( empty($_REQUEST['submit']) ){
			$did = (int) $this->input->get_post('did');
			if($did<=0){
				alert('输入参数错误','back');
				return true;
			}
			
			$dev = $this->dev->getOneById($did);
			$schoolList = $this->school->getSchoolList();
			$devTypes = $this->dev->getDevType();
			
			$temp = array(
					'dev'=>$dev,
					'schoolList'=>$schoolList,
					'devTypes'=>$devTypes,
			);
			$this->showView('admin/dev/detail',$temp);
		}else{
			$id = (int)$this->input->post_get('did');
			$this->devLog->beforeUpdate($id);
			
			$update = array();
			$update['type'] = (int)$this->input->post_get('type');
			$update['schoolID'] = (int)$this->input->post_get('schoolID');
			$where = array('id'=>$id);
			$res = $this->dev->update($where, $update);
			if( $res ){
				$this->devLog->afterUpdate();
				alert('修改成功');
			}else{
				alert('修改失败');
			}
			
		}
		
		
		
	}
	/**
	 * 批量修改学校
	 */
	public function batchUpdateSchool(){
		
		$schoolID = (int) $this->input->post_get('schoolID');
		$idList   = $this->input->post_get('idList');
		if( empty($schoolID) || empty($idList) ){
			$this->apiOutput->errorInput();
		}
		
		$this->devLog->beforeUpdate($idList);
		
		$this->load->database();
		$res = $this->db->set('schoolID',$schoolID)->where_in('id',$idList)->update('hs_dev');
		if ( ! $res ) {
			$this->apiOutput->errorSystem('修改失败');
		}
		
		$this->devLog->afterUpdate();
		$this->apiOutput->put('msg','修改成功')->out();
	}
	
	
	
	
	
	
	
	
	
	
}

